package com.yxinsur.imc.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.yxinsur.imc.business.enums.IsDeletedEnum;
import com.yxinsur.imc.business.mapper.ImcOrderMapper;
import com.yxinsur.imc.business.mapper.ImcOrderRiskMapper;
import com.yxinsur.imc.business.vo.ReportInsCatalogVo;
import com.yxinsur.imc.common.core.domain.TreeSelect;
import com.yxinsur.imc.common.core.domain.entity.InsCatalog;
import com.yxinsur.imc.common.core.domain.entity.SysMenu;
import com.yxinsur.imc.common.core.domain.entity.SysOrg;
import com.yxinsur.imc.common.utils.DateUtils;
import com.yxinsur.imc.common.utils.SecurityUtils;
import com.yxinsur.imc.system.mapper.SysOrgMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yxinsur.imc.business.mapper.ImcInsCatalogMapper;
import com.yxinsur.imc.business.domain.ImcInsCatalog;
import com.yxinsur.imc.business.service.IImcInsCatalogService;

/**
 * 监管报险种分类Service业务层处理
 *
 * @author imc
 * @date 2021-04-27
 */
@Service
public class ImcInsCatalogServiceImpl extends ServiceImpl<ImcInsCatalogMapper, ImcInsCatalog> implements IImcInsCatalogService
{
    @Autowired
    private ImcInsCatalogMapper imcInsCatalogMapper;
    @Autowired
    private SysOrgMapper sysOrgMapper;

    /**
     * 查询监管报险种分类列表
     *
     * @param imcInsCatalog 监管报险种分类
     * @return 监管报险种分类
     */
    @Override
    public List<ImcInsCatalog> selectImcInsCatalogList(ImcInsCatalog imcInsCatalog)
    {
        return imcInsCatalogMapper.selectImcInsCatalogList(imcInsCatalog);
    }

    /**
     * 查询监管报险种分类报表分页数据
     *
     * @return 监管报险种分类报表数据集合
     */
    @Override
    public List<ReportInsCatalogVo> selectInsCatalogReportList(String year,Long orgId) {
        QueryWrapper<ImcInsCatalog> insCatalogQueryWrapper = new QueryWrapper<>();
        insCatalogQueryWrapper.eq("is_deleted", IsDeletedEnum.N.getCode());
        insCatalogQueryWrapper.orderByAsc("id");
        List<ImcInsCatalog> catalogs = this.imcInsCatalogMapper.selectList(insCatalogQueryWrapper);
        List<ReportInsCatalogVo> list = new ArrayList<>();
        List<Long> orgIdList=null;
        if(orgId!=null){
            List<SysOrg> sysOrgs = sysOrgMapper.selectChildrenOrgById(orgId);
            orgIdList=sysOrgs.stream().map(SysOrg::getOrgId).collect(Collectors.toList());
            orgIdList.add(orgId);
        }
        Long tenantId = SecurityUtils.getTenantId();
        for (ImcInsCatalog catalog : catalogs) {
            ReportInsCatalogVo vo = this.imcInsCatalogMapper.selectOrderCatalogReportData(year,orgIdList,catalog.getDepth(),tenantId);
            vo.setCatalogName(catalog.getCatalogName());
            list.add(vo);
        }
        return list;
    }

    /**
     * 查询监管报险种分类树状列表
     *
     * @return 监管报险种分类集合
     */
    @Override
    public List<TreeSelect> getTreeSelectList() {
        QueryWrapper<ImcInsCatalog> catalogQueryWrapper = new QueryWrapper<>();
        catalogQueryWrapper.eq("is_deleted", IsDeletedEnum.N.getCode());
        List<ImcInsCatalog> list = this.imcInsCatalogMapper.selectList(catalogQueryWrapper);
        List<InsCatalog> catalogs = new ArrayList<>();
        list.stream().forEach(a-> {
            InsCatalog log = new InsCatalog();
            BeanUtils.copyProperties(a,log);
            catalogs.add(log);
        });
        List<InsCatalog> menuTrees = buildCatalogTree(catalogs);
        return menuTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }
    private List<InsCatalog> buildCatalogTree(List<InsCatalog> menus)
    {
        List<InsCatalog> returnList = new ArrayList<InsCatalog>();
        List<Long> tempList = new ArrayList<Long>();
        for (InsCatalog dept : menus)
        {
            tempList.add(dept.getId());
        }
        for (Iterator<InsCatalog> iterator = menus.iterator(); iterator.hasNext();)
        {
            InsCatalog menu = (InsCatalog) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(menu.getParentId()))
            {
                recursionFn(menus, menu);
                returnList.add(menu);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = menus;
        }
        return returnList;
    }
    /**
     * 递归列表
     *
     * @param list
     * @param t
     */
    private void recursionFn(List<InsCatalog> list, InsCatalog t)
    {
        // 得到子节点列表
        List<InsCatalog> childList = getChildList(list, t);
        t.setChildren(childList);
        for (InsCatalog tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }
    /**
     * 得到子节点列表
     */
    private List<InsCatalog> getChildList(List<InsCatalog> list, InsCatalog t)
    {
        List<InsCatalog> tlist = new ArrayList<InsCatalog>();
        Iterator<InsCatalog> it = list.iterator();
        while (it.hasNext())
        {
            InsCatalog n = (InsCatalog) it.next();
            if (n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<InsCatalog> list, InsCatalog t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }
}
